<?php


class WebwiMessage
{
    const SUCCESS = 1; // 0001
    const NOTICE  = 2; // 0010
    const ERROR   = 8; // 1000

    const SANITIZE_NONE   = 0;  // 0000 0000
    const SANITIZE_STRING = 16; // 0001 0000
    const SANITIZE_PARAMS = 32; // 0010 0000
    const SANITIZE_BOOTH  = 48; // 0011 0000


	static public $level = array (
        WebwiMessage::SUCCESS => 'success',
        WebwiMessage::NOTICE  => 'notice',
        WebwiMessage::ERROR   => 'error',
    );

    protected $_number = WebwiMessage::NOTICE;

	protected $_string = '';

    /**
     * The formatted message
     *
     * @access  protected
     * @var     string
     */
    protected $_message = '';

    /**
     * Whether the message was already displayed
     *
     * @access  protected
     * @var     boolean
     */
    protected $_is_displayed = false;

    /**
     * Unique id
     *
     * @access  protected
     * @var string
     */
    protected $_hash = null;

    /**
     * holds parameters
     *
     * @access  protected
     * @var     array
     */
    protected $_params = array();

    /**
     * holds additional messages
     *
     * @access  protected
     * @var     array
     */
    protected $_added_messages = array();


    public function __construct($string = '', $number = WebwiMessage::NOTICE,
        $params = array(), $sanitize = WebwiMessage::SANITIZE_NONE)
    {
        $this->setString($string, $sanitize & WebwiMessage::SANITIZE_STRING);
        $this->setNumber($number);
        $this->setParams($params, $sanitize & WebwiMessage::SANITIZE_PARAMS);
    }

    public function __toString()
    {
        return $this->getMessage();
    }

    static public function success($string = '')
    {
        if (empty($string)) {
            $string = __('Your SQL query has been executed successfully');
        }

        return new WebwiMessage($string, WebwiMessage::SUCCESS);
    }



    static public function error($string = '')
    {
        if (empty($string)) {
            $string = __('Error');
        }

        return new WebwiMessage($string, WebwiMessage::ERROR);
    }


    static public function notice($string)
    {
        return new WebwiMessage($string, WebwiMessage::NOTICE);
    }

    static public function raw($message, $type = WebwiMessage::NOTICE)
    {
        $r = new WebwiMessage('', $type);
        $r->setMessage($message);
        return $r;
    }

    static public function affected_rows($rows)
    {
        $message = WebwiMessage::success(_ngettext('%1$d row affected.', '%1$d rows affected.', $rows));
        $message->addParam($rows);
        return $message;
    }
	
	
    static public function deleted_rows($rows)
    {
        $message = WebwiMessage::success(_ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows));
        $message->addParam($rows);
        return $message;
    }	
	
	
    static public function inserted_rows($rows)
    {
        $message = WebwiMessage::success(_ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows));
        $message->addParam($rows);
        return $message;
    }	
	
	
	
	static public function rawError($message)
    {
        return WebwiMessage::raw($message, WebwiMessage::ERROR);
    }
	
    static public function rawNotice($message)
    {
        return WebwiMessage::raw($message, WebwiMessage::NOTICE);
    }	
	
	
    static public function rawSuccess($message)
    {
        return WebwiMessage::raw($message, WebwiMessage::SUCCESS);
    }	
	
    public function isSuccess($set = false)
    {
        if ($set) {
            $this->setNumber(WebwiMessage::SUCCESS);
        }

        return $this->getNumber() === WebwiMessage::SUCCESS;
    }
		
    public function isNotice($set = false)
    {
        if ($set) {
            $this->setNumber(WebwiMessage::NOTICE);
        }

        return $this->getNumber() === WebwiMessage::NOTICE;
    }		
	
    public function isError($set = false)
    {
        if ($set) {
            $this->setNumber(WebwiMessage::ERROR);
        }

        return $this->getNumber() === WebwiMessage::ERROR;
    }	
	
    public function setMessage($message, $sanitize = false)
    {
        if ($sanitize) {
            $message = WebwiMessage::sanitize($message);
        }
        $this->_message = $message;
    }	
		
    public function setString($_string, $sanitize = true)
    {
        if ($sanitize) {
            $_string = WebwiMessage::sanitize($_string);
        }
        $this->_string = $_string;
    }
	
	
    public function setNumber($number)
    {
        $this->_number = $number;
    }	
	
    public function addParam($param, $raw = true)
    {
        if ($param instanceof WebwiMessage) {
            $this->_params[] = $param;
        } elseif ($raw) {
            $this->_params[] = htmlspecialchars($param);
        } else {
            $this->_params[] = WebwiMessage::notice($param);
        }
    }		
	
    public function addString($string, $separator = ' ')
    {
        $this->_added_messages[] = $separator;
        $this->_added_messages[] = WebwiMessage::notice($string);
    }	
	
	
    public function addMessages($messages, $separator = ' ')
    {
        foreach ($messages as $message) {
            $this->addMessage($message, $separator);
        }
    }	
	
    public function addMessage($message, $separator = ' ')
    {
        if (strlen($separator)) {
            $this->_added_messages[] = $separator;
        }

        if ($message instanceof WebwiMessage) {
            $this->_added_messages[] = $message;
        } else {
            $this->_added_messages[] = WebwiMessage::rawNotice($message);
        }
    }	
	
	
    public function setParams($params, $sanitize = false)
    {
        if ($sanitize) {
            $params = WebwiMessage::sanitize($params);
        }
        $this->_params = $params;
    }	
	
	
    public function getParams()
    {
        return $this->_params;
    }	
	
	
    public function getAddedMessages()
    {
        return $this->_added_messages;
    }	
	
    static public function sanitize($message)
    {
        if (is_array($message)) {
            foreach ($message as $key => $val) {
                $message[$key] = WebwiMessage::sanitize($val);
            }

            return $message;
        }

        return htmlspecialchars($message);
    }	
	
	
    static public function decodeBB($message)
    {
        return webwiSanitize($message, false, true);
    }	
	
	
    static public function format()
    {
        $params = func_get_args();
        if (isset($params[1]) && is_array($params[1])) {
            array_unshift($params[1], $params[0]);
            $params = $params[1];
        }

        return call_user_func_array('sprintf', $params);
    }	
	
	
    public function getHash()
    {
        if (null === $this->_hash) {
            $this->_hash = md5(
                $this->getNumber() .
                $this->_string .
                $this->_message
            );
        }

        return $this->_hash;
    }	
	
	
	public function getMessage()
    {
        $message = $this->_message;

        if (0 === strlen($message)) {
            $string = $this->getString();
            if (isset($GLOBALS[$string])) {
                $message = $GLOBALS[$string];
            } elseif (0 === strlen($string)) {
                $message = '';
            } else {
                $message = $string;
            }
        }

        if (count($this->getParams()) > 0) {
            $message = WebwiMessage::format($message, $this->getParams());
        }

        $message = WebwiMessage::decodeBB($message);

        foreach ($this->getAddedMessages() as $add_message) {
            $message .= $add_message;
        }

        return $message;
    }
	
    public function getString()
    {
        return $this->_string;
    }	
	
	
    public function getNumber()
    {
        return $this->_number;
    }	
	
	
    public function getLevel()
    {
        return WebwiMessage::$level[$this->getNumber()];
    }	
	
    public function display()
    {
        echo $this->getDisplay();
        $this->isDisplayed(true);
    }	
	
	
	    public function getDisplay()
    {
        return '<div class="' . $this->getLevel() . '">'
            . $this->getMessage() . '</div>';
    }
	
	
    public function isDisplayed($is_displayed = false)
    {
        if ($is_displayed) {
            $this->_is_displayed = true;
        }

        return $this->_is_displayed;
    }
}
?>